package com.rankeiot.core.excel;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.rankeiot.core.anno.Excel;
import com.rankeiot.core.data.Triple;
import com.rankeiot.core.translator.Translator;
import com.rankeiot.core.util.StringUtil;
import com.rankeiot.core.util.castor.Castor;
import org.sagacity.sqltoy.config.annotation.Translate;
import org.sagacity.sqltoy.utils.BeanUtil;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.text.DecimalFormat;
import java.text.Format;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

import static com.rankeiot.core.excel.TypedRowHandler.excelColumnToNumber;

public class MapRowHandler implements Consumer<DataRow> {
    //获取和设置字段值的代理
    public static class ValueHolder {
        Function<DataRow, Object> getter;
        BiConsumer<Map, Object> setter;
    }

    int startRow = 0;
    final private Map<String, String> columnFieldMapping;
    private List<ValueHolder> fieldValueHolders = new ArrayList<>();
    private Consumer<Map<String, ?>> objHandler;
    Map<String, Translator> translators;

    public MapRowHandler(Map<String, String> columnFieldMapping, Map<String, Translator> translators, Consumer<Map<String, ?>> objHandler) {
        this.columnFieldMapping = columnFieldMapping;
        if (translators == null) {
            this.translators = new HashMap<>();
        } else {
            this.translators = translators;
        }
        this.objHandler = objHandler;
        initFieldValueHolders(null);
    }

    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }

    /**
     * 初始化字段设置器
     *
     * @param row
     */
    private void initFieldValueHolders(DataRow row) {
        for (Map.Entry<String, String> entry : columnFieldMapping.entrySet()) {
            String columnName = entry.getKey();
            String fieldName = entry.getValue();
            char firstCode = columnName.charAt(0);
            int colNum = 0;
            if (firstCode >= 48 && firstCode <= 57) {
                colNum = Integer.valueOf(columnName);
            } else {
                colNum = excelColumnToNumber(columnName);
            }
            ValueHolder vh = new ValueHolder();
            Translator translator = translators.get(fieldName);
            int finalColNum = colNum;
            if (translator == null) {
                vh.getter = (data) -> data.get(finalColNum);
            } else {
                vh.getter = (data) -> translator.translate(data, fieldName,data.get(finalColNum));
            }
            vh.setter = (obj, value) -> obj.put(fieldName, value);
            fieldValueHolders.add(vh);
        }
    }

    @Override
    public void accept(DataRow row) {
        int rowIndex = row.getRowNum();
        if (rowIndex < startRow) {
            return;
        } else {
            Map obj = new HashMap();
            for (ValueHolder vc : fieldValueHolders) {
                Object c = vc.getter.apply(row);
                if(c!=null){
                    if(c instanceof String){
                        if (StringUtil.isNotEmpty((String)c)) {
                            vc.setter.accept(obj, c);
                        }
                    }else{
                        vc.setter.accept(obj, c);
                    }
                }

            }
            if(!obj.isEmpty()){
                objHandler.accept(obj);
            }
        }
    }
}
